const LocalCache = require("./cache/local_cache");
const RawCache = require("./cache/raw_cache");
const SVNCache = require("./cache/svn_cache");
const SimpleLocalCache = require("./cache/simple_local_cache");
const SimpleSVNCache = require("./cache/simple_svn_cache");
// const SimpleSqliteCache = require("./cache/simple_sqlite_cache");

const type2param2mgr = {};
const type2mgrcls = {
    raw: RawCache,
    local: LocalCache,
    svn: SVNCache,
    simple_local: SimpleLocalCache,
    simple_svn: SimpleSVNCache,
    // "simple_sqlite": SimpleSqliteCache,
};

function getCacheMgr(type, param) {
    const param2mgr = type2param2mgr[type] || (type2param2mgr[type] = {});
    const mgr =
        param2mgr[param] || (param2mgr[param] = new type2mgrcls[type](param));
    return mgr;
}

// const defaultCache = new LocalCache();
// const defaultCache = new SVNFileCache();

async function setCache(key, val, type, param) {
    if (!type) {
        type = "local";
    }
    const cacheMgr = getCacheMgr(type, param);
    return await cacheMgr.setCache(key, val);
}

async function getCache(key, type, param) {
    if (!type) {
        type = "local";
    }
    const cacheMgr = getCacheMgr(type, param);
    return await cacheMgr.getCache(key);
}

async function getSize(type, param) {
    const cacheMgr = getCacheMgr(type, param);
    return await cacheMgr.getSize();
}

module.exports = {
    setCache,
    getCache,
    getSize,
};
